package com.cykj.page04;

import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.util.Arrays;

/**
 * @BelongsProject: HandTomcat
 * @BelongsPackage: com.cykj.page04
 * @Author: 阿隆
 * @CreateTime: 2025-10-29 19:09
 * @Description: 通过类对象去操作构造方法
 * @Version: 1.0
 */
public class Lesson02 {
    public static void main(String[] args) {
        try {
            //获取类对象
            Class<?> cls = Class.forName("com.cykj.page04.Student");

            //获取所有的构造方法 (public)
            Constructor<?>[] constructors = cls.getConstructors();

            //获取所有的构造方法 包括 private
            Constructor<?>[] declaredConstructors = cls.getDeclaredConstructors();

            //获取单个构造方法
            Constructor<?> constructor = cls.getDeclaredConstructor(int.class);


            System.out.println(Arrays.toString(constructors));
            System.out.println("-------------------------------");
            System.out.println(Arrays.toString(declaredConstructors));

            //暴力破解
            constructor.setAccessible(true);


            //创建对象
            Student o = (Student)constructor.newInstance(20);

            System.out.println(o.getAge());

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}